<?php

namespace catchAdmin\stock\model;

use catchAdmin\stock\model\search\InventorySearch;
use catcher\base\CatchModel;
use think\facade\Db;
/**
 *
 * @property int $id
 * @property string $inventory_id
 * @property string $config_id
 * @property int $number
 * @property int $box_number
 * @property int $isstatus
 * @property int $createtime
 * @property int $updatetime
 * @property string $status
 * @property int $disc_surplus
 * @property int $disc_deficit
 * @property string $remarks
 * @property int $admin_id
 * @property int $sort
 * @property int $total_number
 */
class InventoryStatement extends CatchModel
{
    use InventorySearch;

    public $field = [
        // ID
        'id',
        'goods_ids',
        'last_month_number',
        'this_month_in_number',
        'this_month_out_number',
        'current_number',
        'current_Good_Qty',
        'current_Hold_Qty',
        'current_NG_Qty',
        'statement_datetime',
        'created_at',
        // 更新时间
        'updated_at',
        // 删除状态，0未删除 >0 已删除
        'deleted_at',
        // 商户id
        'tenant_id',
        // 创建者ID
        'creator_id',
        'is_all_product'
    ];

    public $name = 'db_inventory_statement';

    /**
     *
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this->withoutField(['updated_at'], true)
            ->catchSearch()
            ->where($where)
            ->withoutField('tenant_id,updated_at,deleted_at')
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item){
                $item['goods_name'] = Db::table('db_product')
                    ->where('goods_id','in',explode(',',$item['goods_ids']))
                    ->column('goods_name');

                //处理 年，月
                $item['day_text'] = date("j",strtotime($item['created_at']));
                $item['month_text'] = date("n",strtotime($item['created_at']));
                $item['year_text'] = date("Y",strtotime($item['created_at']));

                $item['operator_name'] = Db::table('users')
                    ->where([['deleted_at','=',0],['id','=',$item['creator_id']]])
                    ->value('username');
            });
    }
}